<?php

namespace app\api\controller\charles;

use app\admin\model\CharlesUser;
use app\api\controller\Base;
use think\Request;

/**
 * 拯救查尔斯用户相关接口
 */
class Cusers extends Base
{

    public $version;
    public $control_version = '0.0.0';

    public function __construct(Request $request = null)
    {
        header('Access-Control-Allow-Origin:*'); // 跨域
        parent::__construct($request);
        $this->version = $this->request->param('version') ?: '';
    }

    // 登录接口
    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');

        $openid = $this->get_openid($gh_id, $code);
        if ($openid) {
            $user = CharlesUser::get(['openid' => $openid]);
            if (!$user) {
                $u = new CharlesUser;
                $u->openid = $openid;
                $u->max_score = 0;
                $u->skin_list = 0;
                $u->skin = 0;
                $u->save();
            }

            $this->result('login', $openid);
        }
        $this->error('error');
    }

    // 获取用户信息
    public function get_user_info()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $nick_name = $this->request->post('nick_name') ?: '';
        $avatar = $this->request->post('avatar') ?: '';

        $user = CharlesUser::get(['openid' => $openid]);
        // 更新昵称头像
        if ($nick_name) {
            $user->nick_name = $nick_name;
        }
        if ($avatar) {
            $user->avatar = $avatar;
        }
        $user->save();
        $data['max_score'] = $user->max_score; // 最高分
        $data['gold'] = $user->gold; // 金币
        $data['skin'] = $user->skin ? $user->skin : 0; // 当前使用的皮肤
        $data['guidance'] = $user->guidance; // 新手指导
        $data['navigate_appid'] = 'wx7c0beb2fc324a64e'; // 跳转小程序appid
        // 审核控制
        $data['flag'] = false;
        if ($this->version == $this->control_version) {
            $data['flag'] = true;
        }
        $this->result('data', $data);
    }

    // 获取跳转小程序
    public function get_navigate_list()
    {
        $list = array(
            array(
                'name' => '一起飞飞飞',
                'appid' => 'wxe42ac5dd0f116b26',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fly.jpg'
            ),
            array(
                'name' => '天天去钓鱼',
                'appid' => 'wxbdd0d9a5187bc731',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fish.jpg'
            ),
            array(
                'name' => '重名查询',
                'appid' => 'wx3093c5ec75efcd7b',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/cmcx2.jpg'
            ),
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/bbjump.jpg'
            ),
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/hbxp.jpg'
            ),
            array(
                'name' => '随你搞',
                'appid' => 'wx07cac980fde4202a',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/sng.jpg'
            ),
            array(
                'name' => '魔盘',
                'appid' => 'wx1ce4c0d710fcec54',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mp.jpg'
            ),
            array(
                'name' => '悦图籍',
                'appid' => 'wxa626926803a7a03b',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/ytj.jpg'
            ),
            array(
                'name' => '精品盒子',
                'appid' => 'wx7c0beb2fc324a64e',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mryw.jpg'
            )
        );
        $this->result('navigate', $list);
    }

    // 更新最高分和金币
    public function update_score()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $score = $this->request->post('score');
        $gold = $this->request->post('gold');
        $user = CharlesUser::get(['openid' => $openid]);
        $user->gold += $gold;
        if ($user->max_score < $score) {
            $user->max_score = $score;
        }
        return $user->save();
    }

    // 获取皮肤信息
    public function get_skin_info()
    {
        $openid = $this->request->param('openid') ?: 'mnq';
        $user = CharlesUser::get(['openid' => $openid]);
        $skin_list = explode(',', $user->skin_list);
        $this->result('get skin info', $skin_list);
    }

    // 购买皮肤
    public function buy_skin()
    {
        $openid = $this->request->param('openid') ?: 'mnq';
        $new_skin = $this->request->param('skin');
        $cost = $this->request->param('cost');

        $user = CharlesUser::get(['openid' => $openid]);
        $gold = $user->gold;
        // 判断金币是否足够花费
        if ($gold < $cost) {
            $this->result('fail');
        }
        $user->skin = $new_skin; // 更换皮肤
        $user->gold -= $cost; // 扣除花费
        $skin_list = explode(',', $user->skin_list);
        // 判断皮肤是否已拥有
        if (!in_array($new_skin, $skin_list)) {
            $user->skin_list = $user->skin_list . ',' . $new_skin;
        }
        $new_skin_list = explode(',', $user->skin_list);
        $data['gold'] = $user->gold;
        $data['skin_list'] = $new_skin_list;
        if ($user->save()) {
            $this->result('ok', $data);
        }
        $this->result('fail');
    }

    // 切换皮肤
    public function change_skin()
    {
        $openid = $this->request->param('openid') ?: 'mnq';
        $new_skin = $this->request->param('skin');

        $user = CharlesUser::get(['openid' => $openid]);
        $skin_list = explode(',', $user->skin_list);

        if (in_array($new_skin, $skin_list)) {
            $user->skin = $new_skin;
            if ($user->save()) {
                $this->result('ok');
            }
        }

        $this->result('fail');
    }

    // 完成新手指导
    public function update_guidance()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $user = CharlesUser::get(['openid' => $openid]);
        $user->guidance = 1;
        $user->save();
    }

    // 获取分享信息
    public function get_share_info()
    {
        $this->result('ok', array(
            'title' => '十面埋伏,看你如何逃出生天',
            'imgUrl' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/charles/share/2.jpg'
        ));
    }

}
